﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public delegate void callbackAndroidChannelPluginInitDone(string msg);

public delegate void callbackAndroidChannelPluginLoginSuccess(WuZiLianZhu.UserInfo user);
public delegate void callbackAndroidChannelPluginLoginFail(string msg);
public delegate void callbackAndroidChannelPluginLoginCancel(string msg);

public delegate void callbackAndroidChannelPluginLogout(string msg);

public delegate void callbackAndroidChannelPluginPaySuccess(string productId);
public delegate void callbackAndroidChannelPluginPayFail(string msg);

public delegate void callbackAndroidChannelPluginCheckPayNeedDelivery(string productId);
public delegate void callbackAndroidChannelPluginCheckPayNothing();


public class AndroidChannelPluginManager : MonoBehaviour {
	const string GameObjectName = "AndroidChannelPluginManager";

	#region Singleton
	static public AndroidChannelPluginManager mInst = null;
    static public AndroidChannelPluginManager Instance(){
		var go = GameObject.Find(GameObjectName);
		if (go == null){
			go = new GameObject(GameObjectName);
		}

		AndroidChannelPluginManager comp = go.GetComponent<AndroidChannelPluginManager>();
		if(comp == null){
			comp = go.AddComponent<AndroidChannelPluginManager>() as AndroidChannelPluginManager;
		}	
		
		mInst = comp;
        return mInst;
    }
	#endregion	

	#region Init
	callbackAndroidChannelPluginInitDone _cbInitDone;
	public void Init(callbackAndroidChannelPluginInitDone cb = null){
		_cbInitDone = cb;
		using(AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")){
			using(AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity")){
				jo.Call("Init");
			}
		}
	}

	public void InitDone(string msg){
		if (_cbInitDone != null){
			_cbInitDone(msg);
		}
	}
	#endregion

	#region Login
	callbackAndroidChannelPluginLoginSuccess _cbLoginSuccess;
	callbackAndroidChannelPluginLoginFail _cbLoginFail;

	public void Login(callbackAndroidChannelPluginLoginSuccess cbSuccess, 
					callbackAndroidChannelPluginLoginFail cbFail)
	{
		_cbLoginSuccess = cbSuccess;
		_cbLoginFail = cbFail;
		
		// 约定在 Plugin 中必须实现 Login 接口
		using(AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")){
			using(AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity")){
				jo.Call("Login");
			}
		}
	}
	
	/*
		约定:
			1）返回的字符串为Json格式的字符串
			2）Json对象含有err字段，类型为string
			3) 如果err为空（Null或者“”），则为登入成功，否则err为错误信息
			4) 登入成功后，会返回id, username等信息，具体字段待定
	*/
	public void CbLoginSuccess(string strJson){
		Debug.Log("CbLoginSuccess: " + strJson);
		try{
			var htJson = MiniJsonExtensions.hashtableFromJson(strJson);
			var user = new WuZiLianZhu.UserInfo();
			user.BindingId = Convert.ToString(htJson["id"]);
			user.Name = Convert.ToString(htJson["username"]);
			_cbLoginSuccess(user);
		
		}catch(Exception e){
			_cbLoginFail(e.ToString());
		}
	}

	public void CbLoginFail(string msg){
		Debug.Log("CbLoginFail: " + msg);
		if (_cbLoginFail != null){
			_cbLoginFail(msg);
		}
	}

	#endregion
	
	#region Logout
	callbackAndroidChannelPluginLogout _cbLogout;
	public void Logout(callbackAndroidChannelPluginLogout cb){
		_cbLogout = cb;

		using(AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")){
			using(AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity")){
				jo.Call("Logout");
			}
		}
	}

	public void CbLogoutDone(string msg){
		Debug.Log("CbLogoutDone: " + msg);

		if(_cbLogout != null){
			_cbLogout(msg);
		}
	}
	#endregion

	#region Pay
	callbackAndroidChannelPluginPaySuccess _cbPaySuccess;
	callbackAndroidChannelPluginPayFail _cbPayFail;
	callbackAndroidChannelPluginLoginCancel _cbPayCancel;
	public void Pay(int price, // 单位： 分
					string productId, // 商品ID，例如：com.tentap.wzlz.Android.GiftBox 这样的形式
					string productName, 
					string productDesc,
					callbackAndroidChannelPluginPaySuccess cbSuccess,
					callbackAndroidChannelPluginPayFail cbFail,
					callbackAndroidChannelPluginLoginCancel cbCancel)
	{
		Debug.Log("AndroidChannelPluginManager::Pay(): " + price + "|" + productId + "|" + productName);
		_cbPaySuccess = cbSuccess;
		_cbPayFail = cbFail;
		_cbPayCancel = cbCancel;
	
		// 约定在 Plugin 中必须实现 Pay 接口
		using(AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")){
			using(AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity")){
				jo.Call("Pay", price, productId, productName, productDesc);
			}
		}
		
	}

	public void CbPaySuccess(string productId){
		Debug.Log("CbPaySuccess: " + productId);
		_cbPaySuccess(productId);
	}

	public void CbPayFail(string msg){
		Debug.Log("CbPayFail: " + msg);
		if (_cbPayFail != null){
			_cbPayFail(msg);
		}
	}

	public void CbPayCancel(string msg){
		Debug.Log("CbPayCancel: " + msg);

		if (_cbPayCancel != null){
			_cbPayCancel(msg);
		}
	}
	#endregion

	#region CheckPay
	callbackAndroidChannelPluginCheckPayNeedDelivery _cbDelivery;
	callbackAndroidChannelPluginCheckPayNothing _cbNothing;

	public void CheckPay(callbackAndroidChannelPluginCheckPayNeedDelivery cb, callbackAndroidChannelPluginCheckPayNothing cbNothing){
		_cbDelivery = cb;
		_cbNothing = cbNothing;
		
		// 约定在 Plugin 中必须实现 Login 接口
		using(AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")){
			using(AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity")){
				jo.Call("CheckPay");
			}
		}

	}

	public void CbCheckPayNeedToDelivery(string productId){
		_cbDelivery (productId);
	}
	
	public void CbCheckPayNothing(){
		_cbNothing ();
	}
	#endregion

	#region Exit
	public void Exit(){
		// 约定在 Plugin 中必须实现 Pay 接口
		using(AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")){
			using(AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity")){
				jo.Call("Exit");
			}
		}
	}
	#endregion
}
